// pages/strategyinfo/strategyinfo.js
var config = require('../../common/config')
var app = getApp()
var sceneres = {}
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载  strategy_detail
   */
  onLoad: function (options) {
    var that = this
    console.log(options)
    var token = wx.getStorageSync('token');
    var open_id = wx.getStorageSync('open_id');
    var pages = getCurrentPages();
    if (pages.length >= 2) {
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        videosferesh: true,
        promotefresh: false
      })
    }
    that.setData({
      nologin: options.nologin || 0
    })
    config.specialpage = 1 //特殊页面，web-view,需要刷新src

    if (options.url){
      var currenturl = decodeURIComponent(decodeURIComponent(options.url));
      if (currenturl) {
        if (currenturl.indexOf('reward.html') > 0 || currenturl.indexOf('videorank.html') > 0 || currenturl.indexOf('league.html') > 0 || options.share == 0) {// 不需要分享
          wx.hideShareMenu({});
        }
        if (currenturl.indexOf('league.html') > 0 || options.login == 0) {// 不需要登录
          that.setData({
            nologin: 1
          })
        }
      }
      if (options.name) {
        var name = decodeURIComponent(options.name);
        that.setData({
          pagetit: name
        })
        wx.setNavigationBarTitle({
          title: name
        })
      }
      if (token) {//unionId
        if (currenturl&&currenturl.indexOf('?') > 0) {
          that.setData({
            shareurl: currenturl,
            weburl: currenturl + '&token=' + token
          })
        } else {
          that.setData({
            shareurl: currenturl,
            weburl: currenturl + '?token=' + token
          })
        }
      } else {
        that.setData({
          shareurl: currenturl,
          weburl: currenturl
        })
      }
    }
    if (options.cid) {
      config.cid = options.cid
      sceneres["cid"] = options.cid
    }
    if (options.fansid) {
      config.partner_id = options.fansid
      sceneres["partner_id"] = options.fansid
    } else if (options.pid) {
      config.partner_id = options.pid
      sceneres["partner_id"] = options.pid
    } else if (options.partner_id) {
      config.partner_id = options.partner_id
      sceneres["partner_id"] = options.partner_id
    }
    console.log(sceneres)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (!this.data.nologin) {
      app.wechatSwitch().then(function (wechat_switch) {//是否打开微信授权开关
        app.commonLogin(sceneres, wechat_switch)
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    config.webresiter = 0
    setTimeout(function(){
      wx.stopPullDownRefresh();
    },1000)
  },
  bindmessage(e) {
    this.setData({
      share_tit: e.detail.data[0].sh_program_title,
      share_img: e.detail.data[0].sh_program_image,
      share_url: e.detail.data[0].sh_program_url,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var that = this, sharetit = '', shareurl;
    var customerId = wx.getStorageSync('customerId');
    if (that.data.pagetit){
      sharetit = that.data.pagetit
    }
    shareurl = '/pages/strategyinfo/strategyinfo?url=' + encodeURIComponent('"https://mobile-test.shihanbainian.com/other/preference.html"') + '&pid=' + customerId
    console.log(that.data.share_tit);
    console.log(that.data.share_img);
    console.log(that.data.share_url);
    return {
      title: that.data.share_tit,
      path: that.data.share_url,
      imageUrl: that.data.share_img,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})